﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;

namespace DingHeng.TransportationManagement.WebCommon.Extension
{
    public static class ControlExtension
    {
        public static void BindData<T>(this DropDownList ddl, List<T> source, string valueField, string textField)
        {
            ddl.DataSource = source;
            ddl.DataValueField = valueField;
            ddl.DataTextField = textField;
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("------", "0"));
        }

        public static void BindData<T>(this CheckBoxList cbl, List<T> source, string valueField, string textField)
        {
            cbl.DataSource = source;
            cbl.DataValueField = valueField;
            cbl.DataTextField = textField;
            cbl.DataBind();
        }

        public static void BindData<T>(this DropDownList ddl, List<T> source)
        {
            ddl.DataSource = source;
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("------", "0"));
        }

        public static void SelectedValue(this DropDownList ddl, string value)
        {
            foreach (ListItem li in ddl.Items)
            {
                if (li.Value.ToLower() == value.ToLower())
                {
                    li.Selected = true;
                    break;
                }
            }
        }
    }
}
